
package org.apache.solr.schema;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SortField;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRef;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.QParser;

public class TrieDateField extends DateField {

    final TrieField wrappedField = new TrieField() {
        {
            type = TrieTypes.DATE;
        }
    };

    @Override
    protected void init(IndexSchema schema, Map<String, String> args) {
        wrappedField.init(schema, args);
        analyzer = wrappedField.analyzer;
        queryAnalyzer = wrappedField.queryAnalyzer;
    }

    @Override
    public Date toObject(IndexableField f) {
        return (Date) wrappedField.toObject(f);
    }

    @Override
    public Object toObject(SchemaField sf, BytesRef term) {
        return wrappedField.toObject(sf, term);
    }

    @Override
    public SortField getSortField(SchemaField field, boolean top) {
        return wrappedField.getSortField(field, top);
    }

    @Override
    public ValueSource getValueSource(SchemaField field, QParser parser) {
        return wrappedField.getValueSource(field, parser);
    }

    /**
     * @return the precisionStep used to index values into the field
     */
    public int getPrecisionStep() {
        return wrappedField.getPrecisionStep();
    }

    @Override
    public void write(TextResponseWriter writer, String name, IndexableField f) throws IOException {
        wrappedField.write(writer, name, f);
    }

    @Override
    public boolean isTokenized() {
        return wrappedField.isTokenized();
    }

    @Override
    public boolean multiValuedFieldCache() {
        return wrappedField.multiValuedFieldCache();
    }

    @Override
    public String storedToReadable(IndexableField f) {
        return wrappedField.storedToReadable(f);
    }

    @Override
    public String readableToIndexed(String val) {
        return wrappedField.readableToIndexed(val);
    }

    @Override
    public String toInternal(String val) {
        return wrappedField.toInternal(val);
    }

    @Override
    public String toExternal(IndexableField f) {
        return wrappedField.toExternal(f);
    }

    @Override
    public String indexedToReadable(String _indexedForm) {
        return wrappedField.indexedToReadable(_indexedForm);
    }

    @Override
    public CharsRef indexedToReadable(BytesRef input, CharsRef charsRef) {
        // TODO: this could be more efficient, but the sortable types should be deprecated instead
        return wrappedField.indexedToReadable(input, charsRef);
    }

    @Override
    public String storedToIndexed(IndexableField f) {
        return wrappedField.storedToIndexed(f);
    }

    @Override
    public IndexableField createField(SchemaField field, Object value, float boost) {
        return wrappedField.createField(field, value, boost);
    }

    @Override
    public Query getRangeQuery(QParser parser, SchemaField field, String min, String max, boolean minInclusive, boolean maxInclusive) {
        return wrappedField.getRangeQuery(parser, field, min, max, minInclusive, maxInclusive);
    }

    @Override
    public Query getRangeQuery(QParser parser, SchemaField sf, Date min, Date max, boolean minInclusive, boolean maxInclusive) {
        return NumericRangeQuery.newLongRange(sf.getName(), wrappedField.precisionStep,
                min == null ? null : min.getTime(),
                max == null ? null : max.getTime(),
                minInclusive, maxInclusive);
    }
}
